package tech.aistar.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import tech.aistar.pojo.Shangpin;
import tech.aistar.service.ShangpinService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 本类用来演示:
 *
 * @author: success
 * @date: 2021/6/21 10:08 上午
 */
@Controller
@RequestMapping("/shangpin")
public class ShangpinController {

    //controller层调用service层
    //service层调用mapper层

    @Autowired
    private ShangpinService shangpinService;

    @GetMapping("/index")
    public String index(HttpServletRequest req,String name){
        List<Shangpin> shangpinList = null;
        //针对name进行校验
        if(null!=name && name.trim().length()!=0){
            //name查询条件是存在的.
            //构建条件对象
            QueryWrapper<Shangpin> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("name",name);

            shangpinList = shangpinService.list(queryWrapper);

            //为了让查询条件进行数据的回显操作
            req.setAttribute("name",name);
        }else{
            //获取product表中所有的数据
            shangpinList = shangpinService.list();
        }

        //将数据库返回出来的数据放入到request作用域中
        //key-value形式
        req.setAttribute("shangpinList",shangpinList);

        //转发到jsp
        return "shangpin/index";
    }
}